import {
  MeshBasicMaterial, PerspectiveCamera,
  Scene, ShaderMaterial, WebGLRenderer
} from "three";
import {
  OrbitControls
} from "three/examples/jsm/controls/OrbitControls";

// interfaces
import { IWord } from '../interfaces/IWord'

import { Basic } from './Basic'
import Sizes from '../Utils/Sizes'
import { Resources } from './Resources';

// earth 
import Earth, { SatelliteData, OrbitType } from './Earth'

export default class World {
  public basic: Basic;
  public scene: Scene;
  public camera: PerspectiveCamera;
  public renderer: WebGLRenderer
  public controls: OrbitControls;
  public sizes: Sizes;
  public material: ShaderMaterial | MeshBasicMaterial;
  public resources: Resources;
  public option: IWord;
  public earth: Earth;


  constructor(option: IWord) {
    /**
     * 加载资源
     */
    this.option = option

    this.basic = new Basic(option.dom)
    this.scene = this.basic.scene
    this.renderer = this.basic.renderer
    this.controls = this.basic.controls
    this.camera = this.basic.camera

    this.sizes = new Sizes({ dom: option.dom })

    this.sizes.$on('resize', () => {
      this.renderer.setSize(Number(this.sizes.viewport.width), Number(this.sizes.viewport.height))
      this.camera.aspect = Number(this.sizes.viewport.width) / Number(this.sizes.viewport.height)
      this.camera.updateProjectionMatrix()
    })

    this.resources = new Resources(async () => {
      await this.createEarth()
      // 开始渲染
      this.render()
    })
  }

  async createEarth() {
    // 资源加载完成，开始制作地球，注释在new Earth()类型里面
    // 根据需求文档设置三层轨道
    const orbits = [
      // 低轨突击层 (600km/35°)
      {
        height: 600,
        eccentricity_rate: 0.001,
        semi_major_axis: 6978,
        dip: 35,
        right_ascension_ascende_node: 30,
        argument_perigee: 0,
        mean_anoma: 0
      },
      // 低轨补充 (太阳同步轨道)
      {
        height: 600,
        eccentricity_rate: 0.001,
        semi_major_axis: 6978,
        dip: 98,
        right_ascension_ascende_node: 60,
        argument_perigee: 0,
        mean_anoma: 0
      },
      // 中轨警戒层 (1400km/60°)
      {
        height: 1400,
        eccentricity_rate: 0.001,
        semi_major_axis: 7814,
        dip: 60,
        right_ascension_ascende_node: 60,
        argument_perigee: 90,
        mean_anoma: 0
      },
      // 中轨极轨道面
      {
        height: 1400,
        eccentricity_rate: 0.001,
        semi_major_axis: 7814,
        dip: 90,
        right_ascension_ascende_node: 0,
        argument_perigee: 90,
        mean_anoma: 0
      },
      // 静轨威慑层 (35786km/0°) - 西太组
      {
        height: 35786,
        eccentricity_rate: 0,
        semi_major_axis: 42164,
        dip: 0,
        right_ascension_ascende_node: 0,
        argument_perigee: 0,
        mean_anoma: 0
      },
      // 静轨威慑层 - 印度洋组
      {
        height: 35786,
        eccentricity_rate: 0,
        semi_major_axis: 42164,
        dip: 0,
        right_ascension_ascende_node: 70,
        argument_perigee: 0,
        mean_anoma: 0
      }
    ];
    
    this.earth = new Earth({
      data: [{
        startArray: {
          name: "基站",
          E: 116.4, // 北京经度
          N: 39.9   // 北京纬度
        },
        endArray: this.option.lonList || []
      }],
      dom: this.option.dom,
      textures: this.resources.textures,
      orbits: orbits,
      earth: {
        radius: 50,
        rotateSpeed: 0.002,
        isRotation: true
      },
      satellite: {
        show: true,
        rotateSpeed: -0.005,
        size: 1,
        number: 2,
        count: this.option.count || 300
      },
      punctuation: {
        circleColor: 0x3892ff,
        lightColumn: {
          startColor: 0xe4007f, // 起点颜色
          endColor: 0xe4007f, // 终点颜色
        },
      },
      flyLine: {
        color: 0xf3ae76, // 飞线的颜色
        flyLineColor: 0xff7714, // 飞行线的颜色
        speed: 0.004, // 拖尾飞线的速度
      },
      satelliteNodes: this.option.satelliteNodes || [],
      onSatelliteClick: this.option.onSatelliteClick // 传递卫星点击回调
    })
    
    // 设置Earth实例的basic属性，使其能访问相机
    this.earth.basic = this.basic;
    
    this.scene.add(this.earth.group)
    await this.earth.init()
  }
  

  
  
  /**
   * 设置轨道显示状态
   * @param orbitType 轨道类型
   * @param visible 是否可见
   */
  public setOrbitVisibility(orbitType: string, visible: boolean) {
    if (this.earth) {
      this.earth.setOrbitVisibility(orbitType, visible);
    }
  }
  
  /**
   * 显示指定轨道层
   * @param orbitType 轨道类型，如果为空则显示所有轨道
   */
  public showOrbitLayer(orbitType?: string) {
    if (this.earth) {
      this.earth.showOrbitLayer(orbitType);
    }
  }

  /**
   * 渲染函数
   */
  public render() {
    requestAnimationFrame(this.render.bind(this))
    this.renderer.render(this.scene, this.camera)
    this.controls && this.controls.update()
    this.earth && this.earth.render()
  }
}